<!--
 * @Author: lihaha 11238063+caimei11@user.noreply.gitee.com
 * @Date: 2022-10-12 14:31:30
 * @LastEditors: lihaha 11238063+caimei11@user.noreply.gitee.com
 * @LastEditTime: 2022-10-16 23:06:01
 * @FilePath: \喜茶\HeyTea_project\HeyTea-ui\src\pages\admin\info\category\List.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE 
-->
<template>
  <!-- 资讯分类管理 -->
  <div>
    <div class="header">
      <el-button type="primary" @click="toAdd">新增</el-button>
      <el-table :data="cateGory">
        <el-table-column
          :index="1"
          type="index"
          label="序号"
          width="80px"
        ></el-table-column>
        <el-table-column prop="name" label="名称" width="180px">
        </el-table-column>
        <el-table-column prop="no" label="排序号" width="100px">
        </el-table-column>
        <el-table-column prop="description" label="描述" align="center">
        </el-table-column>
        <el-table-column label="操作" width="200px" align="center">
          <template slot-scope="scope">
            <el-tag @click="toEdit(scope.row)" type="success">编辑</el-tag
            ><el-tag @click="toDelete(scope.row)" type="danger">删除</el-tag>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 头部结束 -->
    <el-dialog :title="title" :visible.sync="visible" @close="resetForm">
      <el-form ref="myForm" :rules="rule" :model="form">
        <el-form-item label="栏目名称" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="序号" prop="id">
          <el-input v-model="form.id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="栏目介绍">
          <el-input type="textarea" v-model="form.description"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="toClose()">取 消</el-button>
        <el-button type="primary" @click="toSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
  <!-- 模态框 -->
</template>

<script>
import { cateGory, saveOrUpdate, deleteById } from "@/api/proCategory";
export default {
  data() {
    return {
      form: "",
      loading: false,
      title: "",
      visible: false,
      cateGory: [],
      name: "",
      rule: {
        name: [{ required: true, message: "请输入栏目名称", trigger: "blur" }],
      },
    };
  },
  computed: {},
  methods: {
    // 取消模态框关闭
    toClose() {
      setTimeout(() => {
        this.$refs.myForm.resetFields();
        this.visible = false;
      }, 400);
    },
    // 保存或者更新
    toSubmit() {
      this.$refs.myForm.validate(async (valid) => {
        if (valid) {
          // 发送请求
          let res = await saveOrUpdate(this.form);
          this.$notify({
            type: "success",
            message: res.message,
          });
          this.toClose();
          this.getCateGory();
        } else {
          return false;
        }
      });
    },
    // 编辑按钮
    toEdit(row) {
      this.form = { ...row };
      this.title = "修改资讯栏目信息";
      this.visible = true;
    },
    // 查询所有咨询分类
    async getCateGory() {
      let res = await cateGory();
      this.cateGory = res.data;
    },
    // 删除咨询栏目信息
    async toDelete(row) {
      let res = await deleteById(row);
      this.$notify({
        type: "success",
        message: res.message,
      });
      this.getCateGory();
    },

    toAdd() {
      this.title = "新增栏目信息";
      this.visible = true;
      this.form = {};
      this.getCateGory();
    },
    //模态框消失文字取消
    resetForm() {
      this.$refs.myForm.resetFields();
    },
  },

  created() {
    this.getCateGory();
  },
  mounted() {},
};
</script>
<style  lang="scss">
.cell .el-tag {
  margin-right: 10px;
}
</style>